#!/usr/bin/env bash

echo 'start resty building...'

# 阿拉伯数字18作为异常退出码
check='[ $? -eq 0 ] || exit 18'

dir=$(cd $(dirname $0); pwd)

if [ ! -d "lua-resty-moongoo" ];then
  eval "$check" && echo '检出mongo组件lua-resty-moongoo'
  # eval "$check" && git clone https://github.com/isage/lua-resty-moongoo.git
  eval "$check" && echo '因为github在国内检出经常超时，这里使用同步的gitee仓库'
  eval "$check" && git clone https://gitee.com/siusn/lua-resty-moongoo.git
else
  echo "lua-resty-moongoo目录已存在"
fi
eval "$check" && cp -r lua-resty-moongoo/lib/resty/* /usr/local/openresty/lualib/resty


eval "$check" && echo '判断系统是否已经安装了libbson'
if [[ -z $(ldconfig -p | grep libbson) ]];
then
  echo '安装libbson'
  if [[ ! -d "libbson" ]];
  then
    echo "检出libbson"
    # eval "$check" && git clone https://github.com/mongodb/libbson.git
    eval "$check" && echo '因为github在国内检出经常超时，这里使用同步的gitee仓库,如有版本需求可检出指定tag的代码'
    eval "$check" && git clone https://gitee.com/siusn/libbson.git
  else
    echo "libbson目录已存在"
  fi
  eval "$check" && echo "进入libbson目录"
  eval "$check" && cd libbson
  eval "$check" && echo "编译并安装libbson"
  eval "$check" && chmod +x autogen.sh
  eval "$check" && ./autogen.sh
  eval "$check" && make
  eval "$check" && make install
  eval "$check" && make clean && make LUA_INCLUDE_DIR=/usr/local/openresty/luajit/include/luajit-2.1 LUA_CMODULE_DIR=/usr/local/openresty/lualib LUA_MODULE_DIR=/usr/local/openresty/lualib CBSON_CFLAGS="-g -fpic -I/usr/local/include/libbson-1.0/ " CC=cc
  eval "$check" && cd ../
  eval "$check" && ln -s /usr/local/lib/libbson-1.0.so.0 /usr/lib/libbson-1.0.so.0
  eval "$check" && ldconfig
else
  echo "libbson 已安装"
fi


eval "$check" && echo '判断系统是否已经安装了lua-cbson'
if [[ -z $(ldconfig -p | grep lua-cbson) ]];
then
  eval "$check" && echo '安装使用lua-cbson'
  if [[ ! -d "lua-cbson" ]];
  then
    # eval "$check" && git clone https://github.com/isage/lua-cbson.git
    eval "$check" && echo '因为github在国内检出经常超时，这里使用同步的gitee仓库'
    eval "$check" && git clone https://gitee.com/siusn/lua-cbson.git
  else
    echo "lua-cbson目录已存在"
  fi
  eval "$check" && echo '进入lua-cbson目录'
  eval "$check" && cd lua-cbson
  eval "$check" && build_dir="build"
  if [ ! -d "$build_dir" ];
  then
    mkdir $build_dir
  fi
  eval "$check" && cd build
  eval "$check" && echo '编译并安装lua-cbson'
  eval "$check" && cmake ..
  eval "$check" && make
  eval "$check" && make install
  eval "$check" && cd ../../
else
  echo "lua-cbson 已安装"
fi

eval "$check" && echo '更改配置文件并重载'
eval "$check" && cp "$dir"/nginx.conf /usr/local/openresty/nginx/conf
eval "$check" && ic_dir=/usr/local/openresty/nginx/conf/include
eval "$check" && lua_dir=/ying/lua
eval "$check" && lua_lib_dir=/usr/local/openresty/lualib
if [ ! -d "$ic_dir" ];
then
  mkdir $ic_dir
fi
if [ ! -d "$lua_dir" ];
then
  mkdir $lua_dir
fi
eval "$check" && echo '根据部署模式【$MODE】选择要引用的配置文件'
eval "$check" && cp "$dir"/include/"$MODE".conf ${ic_dir}/server.conf
eval "$check" && cp "$dir"/*.lua ${lua_dir}/
eval "$check" && cp "$dir"/config/"$MODE".lua ${lua_lib_dir}/config.lua

if [[ -z $(ps -ef|grep openresty | grep nginx) ]];
then
  eval "$check" && echo '启动openresty'
  eval "$check" && openresty
else
  echo '重载openresty'
  eval "$check" && openresty -s reload
fi

eval "$check" && echo "finish resty building！！！"

